<?php

namespace App\Http\Controllers\Review;

use App\Http\Controllers\Controller;
use App\Models\Review\MenuModel;
use Illuminate\Http\Request;

class MenuController extends Controller
{
    //

    public function newMenu(Request $request){
        $validate=$this->validateForm($request->all(),
        ["name"=>"required|max:100","title"=>"required|max:100"],
        ['name.required' => 'A name is required','name.max' => 'A name is max length is 100','title.required' => 'A title is required','title.max' => 'A title is max length is 100',]);
        if($validate->fails()){
            return $this->errorSend(-1,'invalidate',$validate->errors());
        }
        $data=[
            "name"=>$request['name'],
            "title"=>$request['title'],
            "routepath"=>$request['routepath']??null,
            "url"=>$request['url']??null,
            "params"=>$request['params']??null,
            "permissionid"=>$request['permissionid']??null,
        ];
        $obj=new MenuModel;
        $v=$obj->create($data);
        return $this->passSend(0,'success',$v);
    }
    public function changeMenu(Request $request){
        $id=$request['id']??null;
        if(empty($id)||!is_int($id)){
            return $this->errorSend(-1,'invalidate id',null);
        }
        $obj=MenuModel::find($id);
        $v=0;
        if(!empty($obj)){
         $obj->name=$request['name'];
         $obj->title=$request['title'];
         if(isset($request['routepath']))
         $obj->routepath=$request['routepath'];
         if(isset($request['url']))
         $obj->routepath=$request['url'];
         if(isset($request['params']))
         $obj->routepath=$request['params'];
         if(isset($request['permissionid']))
         $obj->permissionid=$request['permissionid'];
         $v=$obj->save();
        }
        return $this->passSend(0,'success',$v);
    }
    public function deleteMenu(Request $request){
        $id=$request['id']??null;
        if(empty($id)||!is_int($id)){
            return $this->errorSend(-1,'invalidate id',null);
        }
        return $this->passSend(0,'success',MenuModel::destroy($id));
    }
}
